<?php
require_once('../../carbox.php');
if(!carbox_check_session( ))
	header('Location: http://'.$_SERVER['HTTP_HOST'].'/carbox/login.php');

$min = 180;
$inc = 45;
$max = 225;
$colors = NULL;
for($i = 128; $i <= 240; $i += 16) {
	for($j = $min; $j <= $max; $j += $inc)
		$colors[ ] = "background-color: rgb(".$i.", ".$j.", ".$j.");";
	for($j = $min; $j <= $max; $j += $inc)
		$colors[ ] = "background-color: rgb(".$j.", ".$i.", ".$j.");";
	for($j = $min; $j <= $max; $j += $inc)
		$colors[ ] = "background-color: rgb(".$j.", ".$j.", ".$i.");";
	for($j = $min; $j <= $max; $j += $inc)
		$colors[ ] = "background-color: rgb(".$i.", ".$i.", ".$j.");";
	for($j = $min; $j <= $max; $j += $inc)
		$colors[ ] = "background-color: rgb(".$i.", ".$j.", ".$i.");";
	for($j = $min; $j <= $max; $j += $inc)
		$colors[ ] = "background-color: rgb(".$j.", ".$i.", ".$i.");";
}
#for($i = 128; $i <= 240; $i += 16) {
#	for($j = $min; $j <= $max; $j += $inc) {
#		if($j <= 90)
#			$colors[ ] = "background-color: rgb(".$i.", ".$j.", ".$j."); color: white;";
#		else $colors[ ] = "background-color: rgb(".$i.", ".$j.", ".$j.");";
#	}
#	for($j = $min; $j <= $max; $j += $inc) {
#		if($j <= 90)
#			$colors[ ] = "background-color: rgb(".$j.", ".$i.", ".$j."); color: white;";
#		else $colors[ ] = "background-color: rgb(".$j.", ".$i.", ".$j.");";
#	}
#	for($j = $min; $j <= $max; $j += $inc) {
#		if($j <= 90)
#			$colors[ ] = "background-color: rgb(".$j.", ".$j.", ".$i."); color: white;";
#		else $colors[ ] = "background-color: rgb(".$j.", ".$j.", ".$i.");";
#	}
#	for($j = $min; $j <= $max; $j += $inc) {
#		if($j <= 90)
#			$colors[ ] = "background-color: rgb(".$i.", ".$i.", ".$j."); color: white;";
#		else $colors[ ] = "background-color: rgb(".$i.", ".$i.", ".$j.");";
#	}
#	for($j = $min; $j <= $max; $j += $inc) {
#		if($j <= 90)
#			$colors[ ] = "background-color: rgb(".$i.", ".$j.", ".$i."); color: white;";
#		else $colors[ ] = "background-color: rgb(".$i.", ".$j.", ".$i.");";
#	}
#	for($j = $min; $j <= $max; $j += $inc) {
#		if($j <= 90)
#			$colors[ ] = "background-color: rgb(".$j.", ".$i.", ".$i."); color: white;";
#		else $colors[ ] = "background-color: rgb(".$j.", ".$i.", ".$i.");";
#	}
#}
?>
<input type="hidden" id="process_id" value="<?=$_POST['pid']?>" />
<div class="container-fluid">
	<div class="row-fluid">
		<div class="span6">
			<table class="table-summarize" id="table-summarize">
				<thead>
					<tr>
						<th><input type="checkbox" class="checkbox" id="checkbox-select-all" /></th>
						<th>Tipo de Documento</th>
						<th>Página Relativa</th>
						<th>Página Absoluta</th>
						<th>Em Branco</th>
						<th>Excluir</th>
					</tr>
				</thead>
				<tbody>
					<?php
					$sql = "
						SELECT d.id AS document_id, dt.id AS type_id, dt.type, p.id AS page_id, p.number_relative, IFNULL(p.blank, 0) as blank
						FROM Document d
						INNER JOIN DocumentType AS dt ON dt.id = d.type_id
						INNER JOIN Page AS p ON p.document_id = d.id
						WHERE d.process_id = :pid
						ORDER BY d.id, p.number_absolute, p.number_relative
					";
					$query = end(carbox_query($sql, array(":pid" => $_POST["pid"])));
					$c = 0;
					for($i = 0; $i < count($query); ++$i) {
						if($query[$i]["number_relative"] == 1) {
							++$c;
							if($c >= count($colors)-1)
								$c = 0;
						
						}
						echo '
							<tr group-id="'.$query[$i]["document_id"].'" document-id="'.$query[$i]["document_id"].'" style="'.$colors[$c].' border: 1px solid rgb(221, 221, 221)">
								<td style="width: 7%;"><input type="checkbox" class="check-page-id" value="'.$query[$i]["page_id"].'" /></td>
								<td style="width: 50%;" class="doctype" doctype-id="'.$query[$i]["type_id"].'">'.$query[$i]["type"].'</td>
								<td class="relative-page" page="'.$query[$i]["number_relative"].'">'.$query[$i]["number_relative"].'</td>
								<td class="absolute-page">'.($i+1).'</td>
								<td><input type="checkbox" class="check-back-page" '.( intval($query[$i]["blank"] > 0)? 'checked' : '').'/></td>
								<td><input type="checkbox" class="check-exclude" /></td>
							</tr>
						';
					}
					?>
				</tbody>
			</table>
		</div>
		<div class="span6">
			<div class="container-fluid">
				<div class="row-fluid">
					<img src="imgs/tag.png" class="summarize-img span2">
					<fieldset class="span4">
						<legend>Definir Tipo</legend>
						<select class="span12" id="select-doctype" style="margin-right: 0px;">
							<?php
							$query = end(carbox_query("SELECT id, type FROM DocumentType ORDER BY type"));
							for($i = 0; $i < count($query); ++$i) {
								echo '<option value="'.$query[$i]["id"].'">'.$query[$i]["type"].'</option>';
							}
							?>
						</select>
					</fieldset>
					<img src="imgs/confirm.png" class="summarize-img span2">
					<fieldset class="span4">
						<legend>Consolidar</legend>
						<button class="btn btn-primary span12" id="btn-confirm" style="margin-left: 1px;">Confirmar</button>
					</fieldset>
				</div>
				<hr />
				<div class="row-fluid">
					<div class="span12" id="img-preview"><img /></div>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript"> $(carbox_ui_summarize( )); </script>
